package day19.feifei;

public class Ticket {
    private int num;
    private int sum;//已经购买第几张票
public void saleTicket(){
    while (true){
        synchronized (this){
            if(num<=0){
                break;
            }
            num--;
            sum++;
            System.out.println(Thread.currentThread().getName()+"购买了第"+sum+"张票，还剩"+num);
        }

    }
}
    public Ticket() {
    }

    public Ticket(int num, int sum) {
        this.num = num;
        this.sum = sum;
    }


    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getSum() {
        return sum;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }

    public String toString() {
        return "Ticket{num = " + num + ", sum = " + sum + "}";
    }
}

class SaleWindow extends Thread {
    private Ticket ticket;
@Override
    public void run() {
ticket.saleTicket();
    }

    public SaleWindow() {
    }

    public SaleWindow(Ticket ticket,String name) {
    super(name);
        this.ticket = ticket;
    }

    public Ticket getTicket() {
        return ticket;
    }

    public void setTicket(Ticket ticket) {
        this.ticket = ticket;
    }

    public String toString() {
        return "SaleWindow{ticket = " + ticket + "}";
    }
    public static void main(String[] args) {
        Ticket ticket=new Ticket(10,0);
        SaleWindow first=new SaleWindow(ticket,"窗口一");
        SaleWindow second=new SaleWindow(ticket,"窗口二");
        SaleWindow third=new SaleWindow(ticket,"窗口三");
        first.start();
        second.start();
        third.start();
    }
}